function check()
{
	var phone = document.getElementById('phone-input').value;
	if (phone == "")
	{
		return "没有输入电话号码，请检查你的输入。";
	}
	var email = document.getElementById('email-input').value;
	if (email == "")
	{
		return "没有输入邮箱，请检查你的输入。";
	}
	var passord = document.getElementById('password-input').value;
	if (passord == "")
	{
		return "没有输入密码，请检查你的输入。";
	}
	if (passord.length < 8)
	{
		return "密码至少为8位，请检查你的输入。";
	}
	var verification_true = document.getElementById('verification-image').literal;
	var verification_user = document.getElementById('verification-input').value;
	if (verification_user.toUpperCase() != verification_true)
	{
		return "验证码错误。"
	}
	var is_protocol_agree = document.getElementById('protocol-agree-input').checked;
	if (!is_protocol_agree)
	{
		return "请勾选 同意《用户协议》。"
	}
	return "OK";
}

function get_info()
{
	var rtn = check();
	if (rtn != "OK")
	{
		alert(rtn);
		return -1;
	}

	var phone = document.getElementById('phone-input').value;
	var email = document.getElementById('email-input').value;
	var username = document.getElementById('username-input').value;
	var slogan = document.getElementById('slogan-input').value;
	var birthday = document.getElementById('birthday-input').value;
	var is_female = document.getElementById('female-input').checked;
	var nation_input = document.getElementById('nation-input');
	var nation = nation_input.options[nation_input.selectedIndex].value;
	var city_input = document.getElementById('city-input');
	var city = city_input.options[city_input.selectedIndex].value;
	var height = document.getElementById('height-input').value;
	var is_email_agree = document.getElementById('email-agree-input').checked;

	var respect = '帅哥';
	if (is_female)
	{
		respect = '美女';
	}

	var info = '尊敬的' + respect + ' ' + username + ' , 请牢记您的登录信息:\n\n'
	+ '手机号码:   ' + phone + '\n'
	+ '邮箱:   ' + email + '\n'
	+ '个性签名:   ' + slogan + '\n'
	+ '生日:   ' + birthday + '\n'
	+ '国家地区:   ' + nation + ' - ' + city + '\n'
	+ '身高:   ' + height + ' cm'

	if (is_email_agree)
	{
		info += '\n\n' + '我们将为你发送更新邮件。'
	}

	alert(info);
}

function change_city()
{
	var nation_input = document.getElementById('nation-input');
	var nation = nation_input.options[nation_input.selectedIndex].value;
	var city_input = document.getElementById('city-input');
	city_input.options.length=0;

	function write_option(arr)
	{
		for (var idx = 0; idx < arr.length; idx += 1)
		{
			city_input.options.add(new Option(arr[idx]));
		}
	}

	if (nation == '中国')
	{
		var citys = ['北京', '武汉', '上海'];
		write_option(citys);
	}
	else if (nation == '俄罗斯')
	{
		var citys = ['莫斯科', '圣彼得堡'];
		write_option(citys);
	}
	else if (nation == '美国')
	{
		var citys = ['华盛顿', '纽约'];
		write_option(citys);
	}
}

function update_height()
{
	var height = document.getElementById('height-input').value;
	document.getElementById('height-value').innerHTML = height;
}

function display_image()
{
	var img = document.getElementById('image-input').value;
	document.getElementById('image-preview').src = img;
}

function changepic(obj)  // From: CSDN
{
	var newsrc=getObjectURL(obj.files[0]);
	document.getElementById('image-preview').src=newsrc;
}

function getObjectURL(file)  // From: CSDN
{
	var url = null ;
	if (window.createObjectURL!=undefined)
	{
		url = window.createObjectURL(file) ;
	}
	else if (window.URL!=undefined)  // Firefox
	{
		url = window.URL.createObjectURL(file) ;
	}
	else if (window.webkitURL!=undefined)  // Webkit or Chrome
	{
		url = window.webkitURL.createObjectURL(file) ;
	}
	return url ;
}

// The verification code is generate by Mokey Designer.
// See gitee.com/monkeyhbd/designer
function change_verification_code()
{
	var codes = [['./image/ABDEF.png', 'ABDEF'], ['./image/MWHE.png', 'MWHE'],
	             ['./image/MOKEY.png', 'MOKEY'], ['./image/OPEN.png', 'OPEN'],
				 ['./image/GITEE.png', 'GITEE']]
	var idx = Math.floor(Math.random() * 5)

	var target = document.getElementById('verification-image');
	target.src = codes[idx][0];
	target.literal = codes[idx][1];
}
